package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.CompletionClassificationFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.ComparatorsUtils;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;

@Service
public class GrossRegionalProductService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	/**
	 * GDP全市各县区排名情况
	 * @param filter
	 * @return
	 */
	public IndicatorBreakdownDatabean queryCountyRank(
			IndicatorBreakdownFilter filter) {
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists = breakdownService.queryCountyRank(filter);
		System.out.println("size="+lists.size());
		if(lists.size()<3){
			return null;
		}
		IndicatorBreakdownDatabean databeans2 = new IndicatorBreakdownDatabean();
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			DecimalFormat df=new DecimalFormat("#.##");
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			if("0".equals(indicatorBreakdown.getCumulative())){
				databean.setCumulative("0");
			}else {
				String st=df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000);
				databean.setCumulative(st);
			}
			/*if(indicatorBreakdown.getArea().contains("福州市")){
				databeans.add(0, databean);
			}else if(indicatorBreakdown.getArea().contains("蕉城")){
				if(ObjectUtils.isNotBlank(indicatorBreakdown.getExpectedTarget())){
					String st=df.format(Double.parseDouble(indicatorBreakdown.getExpectedTarget())/10000);
					databeans2.setExpectedTarget(st);
				}
				
				
				databeans2.setArea(indicatorBreakdown.getArea());
				databeans.add(2, databean);
			}else if(indicatorBreakdown.getArea().contains("古田县")||indicatorBreakdown.getArea().contains("周宁县")
					||indicatorBreakdown.getArea().contains("寿宁县")||indicatorBreakdown.getArea().contains("屏南县")
					||indicatorBreakdown.getArea().contains("柘荣县")||indicatorBreakdown.getArea().contains("福安市")
					||indicatorBreakdown.getArea().contains("福鼎市")||indicatorBreakdown.getArea().contains("晋安区")||indicatorBreakdown.getArea().contains("霞浦县")){
				databeans.add(databean);
			}*/
			databeans.add(databean);
		}
		databeans2.setRightdata(databeans);
		return databeans2;
	}
	/**
	 *  GDP当季同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> lists2 = breakdownService.queryMonthDevelopmentSituation(filter);
		//校验
		if(lists2.size()<2){
			return null;
		}
		
		String lastDateVal = "";
		String DateVal = "";
		IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
		for (IndicatorBreakdown indicatorBreakdown : lists2) {
			
			if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -1))){
				lastDateVal = indicatorBreakdown.getCumulative();
			}else {
				DateVal = indicatorBreakdown.getCumulative();
				databean.setRatio(indicatorBreakdown.getRatio());
				databean.setCountyIncrease(indicatorBreakdown.getExpectedTarget());
				databean.setCityIncrease(indicatorBreakdown.getCountyIncrease());
			}
			
		}
		breakdownDatabean2s.add(databean);
		return breakdownDatabean2s;
	}
	/**
	 * GDP各季度绝对值及同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	public List<Map<String, Object>> queryAbsoluteValue(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		List<IndicatorBreakdownDatabean> breakdownDatabeans2 = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans3 = new ArrayList<IndicatorBreakdownDatabean>();
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailData(breakdownSs,breakdownDatabeans);
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			String date = breakdownDatabean.getCurrentMonth();
			for (IndicatorBreakdownDatabean databean : breakdownDatabeans) {
				if(databean.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(date,-12))){
					Double double5 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double6 = Double.parseDouble(databean.getSeasonsComparison());
					Double double7 = (double5-double6)/double6*100;
					if(double7 != 0){
						DecimalFormat df=new DecimalFormat("#.##");
						String st=df.format(double7);
						breakdownDatabean.setConcurrentGrowth(st);
					}
				}
			}
			breakdownDatabean.setSeasonsComparison(breakdownDatabean.getSeasonsComparison().substring(0, 2));
			if(breakdownDatabean.getCurrentMonth().contains("2016")){
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans2.add(breakdownDatabean);
			}else {
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans3.add(breakdownDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabeans2);
		Collections.reverse(breakdownDatabeans3);
		map2.put("lastYear", breakdownDatabeans2);
		map2.put("years", breakdownDatabeans3);
		list.add(map2);
		return list;
	}
	/**
	 * GDP累计同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	public List<Map<String, Object>> queryCumulativeGrowth(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdownSs,breakdownDatabeans);
		DecimalFormat df=new DecimalFormat("#.##");
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabeans) {
			indicatorBreakdown.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			if(indicatorBreakdown.getCurrentMonth().contains(filter.getDate().substring(0,4))){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		if(ObjectUtils.isNotBlank(breakdowns.get(0).getExpectedTarget())){
			String st=df.format(Double.parseDouble(breakdowns.get(0).getExpectedTarget())/10000);
			map32.put("expectedTarget", st);
		}
		
		list32.add(map32);
		return list32;
	}
	/**
	 * 第一产业完成情况
	 * @param filter
	 * @return
	 */
	public List<Map<String, Object>> queryByXZValue(
			IndicatorBreakdownFilter filter) {
		CompletionClassificationFilter classificationFilter = new CompletionClassificationFilter();
		classificationFilter.setArea(filter.getArea());
		classificationFilter.setIndicatorName(filter.getIndustryName());
		classificationFilter.setIndustryId(filter.getIndustryId());
		classificationFilter.setCategory("行业");
		classificationFilter.setCategoryDetails(filter.getXz());
		classificationFilter.setCompletionPeriod(filter.getDate());
		classificationFilter.setYear(filter.getCurrentMonth().substring(0, 4));
		List<CompletionClassification> lists =  classificationService.queryByfilter(classificationFilter);
		if(ObjectUtils.isBlank(lists)){
			return null;
		}
		List<CompletionClassificationDatabean> databeans = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassification completionClassification : lists) {
			CompletionClassificationDatabean databean = new CompletionClassificationDatabean();
			databean.setActualCompletionValue(Double.toString((Double.parseDouble(completionClassification.getActualCompletionValue())/10000)).substring(0,4));
			Double double1 = Double.parseDouble(completionClassification.getPlanneValue());
			Double double2 = Double.parseDouble(completionClassification.getActualCompletionValue());
			databean.setNoComplete(Double.toString(((double1-double2)/10000)).substring(0,4));
			databean.setSpecificIncrease(completionClassification.getSpecificIncrease());
			databean.setYear(completionClassification.getYear());
			databean.setCompleteAnnual(completionClassification.getCompleteAnnual());
			databean.setCompletionPeriod(completionClassification.getCompletionPeriod());
			databeans.add(databean);
		}
//		Collections.sort(databeans, new ComparatorsUtils()); 
		Collections.reverse(databeans);
		List<CompletionClassificationDatabean> databean1s = new ArrayList<CompletionClassificationDatabean>();
		List<CompletionClassificationDatabean> databean2s = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassificationDatabean completionClassificationDatabean : databeans) {
			if(completionClassificationDatabean.getYear().equals(classificationFilter.getYear())){
				databean1s.add(completionClassificationDatabean);
			}else {
				databean2s.add(completionClassificationDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("years", databean1s);
		map.put("lastYear", databean2s);
		list.add(map);
		return list;
	}
	/**
	 * 第二产业完成情况
	 * @param filter
	 * @return
	 */
	public List<Map<String, Object>> queryByBMValue(
			IndicatorBreakdownFilter filter) {
		CompletionClassificationFilter classificationFilter = new CompletionClassificationFilter();
		classificationFilter.setArea(filter.getArea());
		classificationFilter.setIndicatorName(filter.getIndustryName());
		classificationFilter.setIndustryId(filter.getIndustryId());
		classificationFilter.setCategory("行业");
		classificationFilter.setCategoryDetails(filter.getBm());
		classificationFilter.setCompletionPeriod(filter.getDate());
		classificationFilter.setYear(filter.getCurrentMonth().substring(0, 4));
		List<CompletionClassification> lists =  classificationService.queryByfilter(classificationFilter);
		if(ObjectUtils.isBlank(lists)){
			return null;
		}
		List<CompletionClassificationDatabean> databeans = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassification completionClassification : lists) {
			CompletionClassificationDatabean databean = new CompletionClassificationDatabean();
			databean.setActualCompletionValue(Double.toString((Double.parseDouble(completionClassification.getActualCompletionValue())/10000)).substring(0,4));
			Double double1 = Double.parseDouble(completionClassification.getPlanneValue());
			Double double2 = Double.parseDouble(completionClassification.getActualCompletionValue());
			databean.setNoComplete(Double.toString(((double1-double2)/10000)).substring(0,4));
			databean.setSpecificIncrease(completionClassification.getSpecificIncrease());
			databean.setYear(completionClassification.getYear());
			databean.setCompleteAnnual(completionClassification.getCompleteAnnual());
			databean.setCompletionPeriod(completionClassification.getCompletionPeriod());
			databeans.add(databean);
		}
//		Collections.sort(databeans, new ComparatorsUtils()); 
		Collections.reverse(databeans);
		List<CompletionClassificationDatabean> databean1s = new ArrayList<CompletionClassificationDatabean>();
		List<CompletionClassificationDatabean> databean2s = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassificationDatabean completionClassificationDatabean : databeans) {
			if(completionClassificationDatabean.getYear().equals(classificationFilter.getYear())){
				databean1s.add(completionClassificationDatabean);
			}else {
				databean2s.add(completionClassificationDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("years", databean1s);
		map.put("lastYear", databean2s);
		list.add(map);
		return list;
	}
	/**
	 * 第三产业完成情况
	 * @param filter
	 * @return
	 */
	public List<Map<String, Object>> queryByHYValue(
			IndicatorBreakdownFilter filter) {
		CompletionClassificationFilter classificationFilter = new CompletionClassificationFilter();
		classificationFilter.setArea(filter.getArea());
		classificationFilter.setIndicatorName(filter.getIndustryName());
		classificationFilter.setIndustryId(filter.getIndustryId());
		classificationFilter.setCategory("行业");
		classificationFilter.setCategoryDetails(filter.getHy());
		classificationFilter.setCompletionPeriod(filter.getDate());
		classificationFilter.setYear(filter.getCurrentMonth().substring(0, 4));
		List<CompletionClassification> lists =  classificationService.queryByfilter(classificationFilter);
		if(ObjectUtils.isBlank(lists)){
			return null;
		}
		List<CompletionClassificationDatabean> databeans = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassification completionClassification : lists) {
			CompletionClassificationDatabean databean = new CompletionClassificationDatabean();
			databean.setActualCompletionValue(Double.toString((Double.parseDouble(completionClassification.getActualCompletionValue())/10000)).substring(0,4));
			Double double1 = Double.parseDouble(completionClassification.getPlanneValue());
			Double double2 = Double.parseDouble(completionClassification.getActualCompletionValue());
			databean.setNoComplete(Double.toString(((double1-double2)/10000)).substring(0,4));
			databean.setSpecificIncrease(completionClassification.getSpecificIncrease());
			databean.setYear(completionClassification.getYear());
			databean.setCompleteAnnual(completionClassification.getCompleteAnnual());
			databean.setCompletionPeriod(completionClassification.getCompletionPeriod());
			databeans.add(databean);
		}
//		Collections.sort(databeans, new ComparatorsUtils()); 
		Collections.reverse(databeans);
		List<CompletionClassificationDatabean> databean1s = new ArrayList<CompletionClassificationDatabean>();
		List<CompletionClassificationDatabean> databean2s = new ArrayList<CompletionClassificationDatabean>();
		for (CompletionClassificationDatabean completionClassificationDatabean : databeans) {
			if(completionClassificationDatabean.getYear().equals(classificationFilter.getYear())){
				databean1s.add(completionClassificationDatabean);
			}else {
				databean2s.add(completionClassificationDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("years", databean1s);
		map.put("lastYear", databean2s);
		list.add(map);
		return list;
	}
	
	
}
